<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\PatientCase;
use Redirect,Input;
use App\User;
use App\PatientInfo;
use Auth,DB;

class CaseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create($id)
    {
        //    
        $patient = PatientInfo::find($id);

        return view('user.case.create')->with('patient',$patient);
	}
    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'firstname'=>'required',
            'lastname' =>'required',
            '_patient_id' => 'required',     
            ]);
        
        $patientCase = new PatientCase;
       
        $patientCase->user_id = Auth::user()->id;
        $patientCase->patient_id = Input::get('_patient_id');
        $patientCase->first_name = Input::get('firstname');
        $patientCase->last_name = Input::get('lastname');
        $patientCase->eyesight_left = Input::get('eyeSightLeft');
        $patientCase->eyesight_right = Input::get('eyeSightRight');
        $patientCase->description = trim(Input::get('caseDescription'));
        $patientCase->type = Input::get('caseType');

        if($patientCase->save()){
            return Redirect::to('patient/info/'.$patientCase->patient_id);
        } else {
            return Redirect::back()->withInput()->withErrors('Save Failed');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $case = PatientCase::find($id);
        return view('user.case.edit')->with('case', $case);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request,$id)
    {
        
        $this->validate($request,[
            'firstname'=>'required',
            'lastname' =>'required',
            '_patient_id' =>'required',      
            ]);
        
        $patientCase = PatientCase::find($id);
        $patientCase->patient_id = Input::get('_patient_id');
        $patientCase->first_name = Input::get('firstname');
        $patientCase->last_name = Input::get('lastname');
        $patientCase->eyesight_left = Input::get('eyeSightLeft');
        $patientCase->eyesight_right = Input::get('eyeSightRight');
        $patientCase->description = Input::get('caseDescription');
        $patientCase->type = Input::get('caseType');

        if($patientCase->save()){
            return Redirect::to('patient/info/'.$patientCase->patient_id.'/edit');
        } else {
            return Redirect::back()->withErrors('Save Failed');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $case = PatientCase::find($id);

        $case->delete();

        return Redirect::back();
        
    }
}
